﻿using PSI.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PSI.Services.TMS {
    /// <summary>
    /// 导入导出服务
    /// </summary>
    public class ImportService : PSIBaseService {
        /// <summary>
        /// 导入业务员
        /// </summary>
        /// <param name="bizMan"></param>
        public void ImportBizMan(string bizMan) {
            if (string.IsNullOrEmpty(bizMan)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_biz_man where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", bizMan);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_biz_man(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = bizMan;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportVsl(string vsl) {
            if (string.IsNullOrEmpty(vsl)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_vsl where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", vsl);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_vsl(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = vsl;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportClients(string clients) {
            if (string.IsNullOrEmpty(clients)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_clients where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", clients);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_clients(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = clients;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportCntOwner(string cntOnwer) {
            if (string.IsNullOrEmpty(cntOnwer)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_cnt_owner where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", cntOnwer);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_cnt_owner(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = cntOnwer;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportAddress(string address) {
            if (string.IsNullOrEmpty(address)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_address where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", address);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_address(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = address;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportCustomer(string customer) {
            if (string.IsNullOrEmpty(customer)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_customer where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", customer);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_customer(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = customer;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportCntType(string cntType) {
            if (string.IsNullOrEmpty(cntType)) {
                return;
            }

            var sql = @"select count(*) from tms_bc_cnt_type where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", cntType);
            var cnt = QueryCount(sql, p);
            if (cnt > 0) {
                return;
            }

            sql = @"insert into tms_bc_cnt_type(id, f_name, create_user_id, create_datetime,
                        org_data, pinyin) values(@id, @name, @userId, getdate(), @orgData, @pinyin)";
            var id = IdGeneratorService.NewId();
            var name = cntType;
            var userId = UserService.GetLoginUser().Id;
            var orgData = new UserService().GetUser(userId).OrgCode;
            var pinyin = new PinYinHelper().ConvertToPinYinZiTou(name, true);
            p = CreateDbParameters();
            p.AddWithValue("id", id);
            p.AddWithValue("name", name);
            p.AddWithValue("userId", userId);
            p.AddWithValue("orgData", orgData);
            p.AddWithValue("pinyin", pinyin);
            Execute(sql, p);
        }

        public void ImportTruck(string truckCompany, string truckNumber, string driver, string driverTel) {
            if (string.IsNullOrEmpty(truckCompany)) {
                return;
            }
            if (string.IsNullOrEmpty(truckNumber)) {
                return;
            }

            if (truckCompany == truckNumber) {
                truckCompany = "营口陆港物流";
            }

            var sql = @"select id from tms_truck_company where f_name = @name";
            var p = CreateDbParameters();
            p.AddWithValue("name", truckCompany);
            string tcId = string.Empty;
            Query(sql, p, reader => {
                tcId = reader.GetString(0);
            });
            if (string.IsNullOrEmpty(tcId)) {
                tcId = IdGeneratorService.NewId();

                sql = "insert into tms_truck_company(id, f_name, f_pinyin) values (@id, @name, @pinyin)";
                p = CreateDbParameters();
                p.AddWithValue("id", tcId);
                p.AddWithValue("name", truckCompany);
                p.AddWithValue("pinyin", new PinYinHelper().ConvertToPinYinZiTou(truckCompany, true));
                Execute(sql, p);
            }

            sql = "select count(*) from tms_truck where parent_id = @id and f_truck_num = @truckNumber";
            p = CreateDbParameters();
            p.AddWithValue("id", tcId);
            p.AddWithValue("truckNumber", truckNumber);
            var cnt = QueryCount(sql, p);
            if (cnt == 0) {
                sql = @"insert into tms_truck(id, parent_id, f_truck_num, f_driver, f_driver_pinyin, f_driver_tel, f_truck_num_pinyin)
                        values (@id, @parentId, @truckNumber, @driver, @driverPinyin, @driverTel, @truckNumberPinyin)";
                p = CreateDbParameters();
                p.AddWithValue("id", IdGeneratorService.NewId());
                p.AddWithValue("parentId", tcId);
                p.AddWithValue("truckNumber", truckNumber);
                p.AddWithValue("driver", driver);
                p.AddWithValue("driverPinyin", new PinYinHelper().ConvertToPinYinZiTou(driver, true));
                p.AddWithValue("driverTel", driverTel);
                p.AddWithValue("truckNumberPinyin", new PinYinHelper().ConvertToPinYinZiTou(truckNumber, true));

                Execute(sql, p);
            }
        }
    }
}